import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'home',
    component: () =>
        import ('@/views/home/home.vue' /* webpackChunkName: "home" */ )
}, {
    path: '/discovery',
    name: 'discovery',
    component: () =>
        import ('@/views/discovery/discovery' /* webpackChunkName: "discovery" */ )
}, {
    path: '/toplist',
    name: 'toplist',
    component: () =>
        import ('@/views/toplist/toplist' /* webpackChunkName: "toplist" */ )
}, {
    path: '/news',
    name: 'news',
    component: () =>
        import ('@/views/news/news' /* webpackChunkName: "news" */ )
}, {
    path: '/seek',
    name: 'seek',
    component: () =>
        import ('@/views/seek/seek' /* webpackChunkName: "seek" */ )
}, {
    path: '/login',
    name: 'login',
    component: () =>
        import ('@/views/login/login' /* webpackChunkName: "login" */ )
}, {
    path: '/register',
    name: 'register',
    component: () =>
        import ('@/views/register/register' /* webpackChunkName: "register" */ )
}, {
    path: '/detail',
    name: 'detail',
    component: () =>
        import ('@/views/detail/Detail' /* webpackChunkName: "Detail" */ )
}, {
    path: '/user',
    name: 'user',
    component: () =>
        import ('@/views/user/user' /* webpackChunkName: "user" */ )
}]

const router = new VueRouter({

        routes
    })
    //  路由守卫
router.beforeEach((to, from, next) => {
        const isLogin = localStorage.username ? true : false
            // 不在登录状态下进入个人中心时，跳转到登录页面
        if (to.path === '/user' && !isLogin) {
            next('/login')
        } else {
            next()
        }
    })
    //获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
    //修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export default router